(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.popup.serviceEventController', viewController);
	viewController.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService','ajaxServiceFactory','loggerFactory', '$filter' ];
	function viewController($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, validatorConfig, authService,ajaxServiceFactory,loggerFactory, $filter) {
		var ctrl = this, params, oldEventNotifyTpl = {}, oldEventNotifyTpls = [];
		ctrl.dialogsModule = dialogsModule;
		params = ctrl.dialogsModule.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		ctrl.localeCodeChanged = function() {
			if(!ctrl.eventNotifyTpl) return;
			var value = ctrl.eventNotifyTpl.localeCode;
			var keepGoing = true;
			angular.forEach(oldEventNotifyTpls, function(eventNotifyTpl) {
				if(keepGoing) {
					if($filter("encode")(eventNotifyTpl.localeCode) == $filter("encode")(value) 
							&& oldEventNotifyTpl.notifyParty == eventNotifyTpl.notifyParty 
							&& oldEventNotifyTpl.notifyType == eventNotifyTpl.notifyType) {
						ctrl.eventNotifyTpl = angular.copy(eventNotifyTpl);
						oldEventNotifyTpl = {};
						angular.copy(ctrl.eventNotifyTpl, oldEventNotifyTpl);
						keepGoing = false;
					} else {
						ctrl.eventNotifyTpl.notifyTitle = null;
						ctrl.eventNotifyTpl.notifyContent = null;
					}
				}
			});
		};
		ctrl.notifyTypeChanged = function() {
			if(!ctrl.eventNotifyTpl) return;
			var value = ctrl.eventNotifyTpl.notifyType;
			var keepGoing = true;
			angular.forEach(oldEventNotifyTpls, function(eventNotifyTpl) {
				if(keepGoing) {
					if($filter("encode")(oldEventNotifyTpl.localeCode) == $filter("encode")(eventNotifyTpl.localeCode) 
							&& oldEventNotifyTpl.notifyParty == eventNotifyTpl.notifyParty
							&& eventNotifyTpl.notifyType == value) {
						ctrl.eventNotifyTpl =  angular.copy(eventNotifyTpl);
						oldEventNotifyTpl = {};
						angular.copy(ctrl.eventNotifyTpl, oldEventNotifyTpl);
						keepGoing = false;
					} else {
						ctrl.eventNotifyTpl.notifyTitle = null;
						ctrl.eventNotifyTpl.notifyContent = null;
					}
				}
			});
		};
		ctrl.notifyPartyChanged = function() {
			if(!ctrl.eventNotifyTpl) return;
			var value = ctrl.eventNotifyTpl.notifyParty;
			var keepGoing = true;
			angular.forEach(oldEventNotifyTpls, function(eventNotifyTpl) {
				if(keepGoing) {
					if($filter("encode")(oldEventNotifyTpl.localeCode) == $filter("encode")(eventNotifyTpl.localeCode)
							&& eventNotifyTpl.notifyParty == value 
							&& oldEventNotifyTpl.notifyType == eventNotifyTpl.notifyType) {
						ctrl.eventNotifyTpl =  angular.copy(eventNotifyTpl);
						oldEventNotifyTpl = {};
						angular.copy(ctrl.eventNotifyTpl, oldEventNotifyTpl);
						keepGoing = false;
					} else {
						ctrl.eventNotifyTpl.notifyTitle = null;
						ctrl.eventNotifyTpl.notifyContent = null;
					}
				}
			});
		};
		var ajaxService = ajaxServiceFactory.buildAjaxService({
			dropDownDataOptionsForEventNotifyTpl : {
				url : 'do/cda/service/dropDownDataOptionsForEventNotifyTpl',
				method : 'post'
			}
		});
		function dropDownDataOptionsForEventNotifyTpl(params) {
			var defer = $q.defer();
			ajaxService.dropDownDataOptionsForEventNotifyTpl(params).then(function(response) {
				var options = {};
				if (response.data) {
					angular.forEach(response.data, function(value, key) {
						var tempValue = {};
						tempValue = value;
						options[key] = tempValue;
					});
				}
				defer.resolve(options);
			});
			return defer.promise;
		}
		ctrl.init = function() {
			ctrl.innerGridOptionsSubEvent = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.subEvent.tableConfigs.table, rowSelectionChanged);
			ctrl.innerGridOptionsSubEvent.multiSelect = false;
			ctrl.innerGridOptionsSubEvent.enableSelectAll = false;
			ctrl.innerGridOptionsSubEvent.minRowsToShow = 4,
			ctrl.innerGridOptionsSubEvent.data = 'vm.serviceEvent.subEvents';
			ctrl.subEventSelected = false;
			//init EventNotifyTplParam
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.serviceEventNotifyTplParams.tableConfigs.table);
			ctrl.innerGridOptions.data = 'vm.serviceEvent.notifyTplParams';
			function getServiceStatusOptions() {
				var data = [ {
					"label" : "label.service.active",
					"value" : "A"
				}, {
					"label" : "label.service.inactive",
					"value" : "I"
				}];
				return data;
			}
			ctrl.eventStatusOptions = getServiceStatusOptions();
			
			dropDownDataOptionsForEventNotifyTpl(params).then(function(response) {
				ctrl.localeCodeOptions = response.localeCodeOptions;
				ctrl.notifyTypeOptions = response.notifyTypeOptions;
				ctrl.notifyPartyOptions = response.notifyPartyOptions;
				
				authService.viewEvent(params).then(function(response) {
					ctrl.serviceEvent = response.data || params;
					ctrl.serviceEvent.subEvents = ctrl.serviceEvent.serviceSubEvents;
					ctrl.serviceEvent.notifyTplParams = ctrl.serviceEvent.notifyTplParams;
					
					var eventNotifyTpls = ctrl.serviceEvent.NotifyTpls;
					ctrl.eventNotifyTpl = eventNotifyTpls[0];
					oldEventNotifyTpls = [];
					angular.copy(eventNotifyTpls, oldEventNotifyTpls);
					oldEventNotifyTpl = {};
					angular.copy(ctrl.eventNotifyTpl, oldEventNotifyTpl);
				}, angular.noop);
			}, angular.noop);
		};

		function rowSelectionChanged(eventRow, selectedRows) {
			if (!selectedRows || selectedRows.length == 0) { 
				ctrl.subEventSelected = false;
				ctrl.serviceSubEventInfo = {};
			} else {
				ctrl.subEventSelected = true;
				ctrl.serviceSubEventInfo = selectedRows[0];
			}
		}
	}
})(angular);